{* Smarty *}
<script type="text/javascript" language="Javascript1.2">
{php}
sajax_show_javascript();
{/php}
{literal}
// --- [ Select factories ] ---------------------
var num_form = 1;

function do_items_cb(ss)
{
    var f = document.forms["form" + num_form];
    var e = f.elements["items_list[]"];
    e.options.length=0;

    if("" != ss)
    {
        var z = "asd"; z = ss;
        z = z.split("~~");
        idx = 0;
        for (v in z)
        {
            vls = z[v].split("|");
            e.options[idx++] = new Option(vls[1], vls[0]); //, vls[1], vls[1]);
        }
    } // end if
}


function do_list_items(n_f)
{
    num_form = n_f;
    s = "";
    var f = document.forms["form" + num_form];
    var e = f.elements["catalog"];

    for(i=0; i<e.options.length; i++)
    {
        if(e.options[i].selected)
        {
            s += (s=="" ? "" : "|")+e.options[i].value;
        }
    }
    x_GetListProducts(s, do_items_cb);
}


</script>

<script language="javascript">
function AddItem(n_f)
{
    num_form = n_f;
    var f = document.forms["form" + num_form];
    if(!f) 
    {
        alert("Unknown error!");
        return false;
    }
    
    var e1 = f.elements['items_list[]']; 
    var e2 = f.elements['selected_items_list[]'];
    for (i = 0; i < e1.length; i++)
    {
        if (e1[i].selected)
        {
            flag = true;
            for (j = 0; j < e2.length; j++)
                if (e2[j].value == e1[i].value)
                {
                    flag = false;
                    break;
                }

            if (flag)
                e2.options[e2.length] = new Option(e1[i].text, e1[i].value);
        }
    }
}



function AddCatalog(n_f)
{
    num_form = n_f;
    var f = document.forms["form" + num_form];
    if(!f) 
    {
        alert("Unknown error!");
        return false;
    }
    
    var e1 =  f.elements['catalog'].options[f.elements['catalog'].selectedIndex];
    var e2 = f.elements['selected_items_list[]'];
    
    
    if (0 != e1.value)
    {
        flag = true;
        for (j = 0; j < e2.length; j++)
            if (e2[j].value == e1.value)
            {
                flag = false;
                break;
            }

        if (flag)
            e2.options[e2.length] = new Option(e1.text, e1.value);
    }
}



function DellItem(n_f)
{
    num_form = n_f;
    var f = document.forms["form" + num_form];
    if(!f) 
    {
        alert("Unknown error!");
        return false;
    }
    
    var e = f.elements['selected_items_list[]'];
    for (i = e.length - 1; i >= 0; i--)
    {
        if (e[i].selected)
        {
             e.options[i] = null;
        }
    }
}


function CheckData(n_f)
{
    num_form = n_f;
    var f = document.forms["form" + num_form];
    if(!f)
    {
        alert("Unknown error!");
        return false;
    }
    
    var re = new RegExp("^[0-9]*[0-9]$");
    if (f.elements['discount_code'] && "" == f.elements['discount_code'].value)
    {
        alert("Discount Code is empty.");
        return false;
    }

    if ("" == f.elements['discount_title'].value)
    {
        alert("Discount Title is empty.");
        return false;
    }

    if (re.exec(f.elements['discount_amount'].value) == null)
    {
        alert("Discount Amount is empty or incorrect.");
        return false;
    }

    if (f.elements['discount_pr_count'] && re.exec(f.elements['discount_pr_count'].value) == null)
    {
        alert("Discount Price/Count is empty or incorrect.");
        return false;
    }

    if ("" == f.elements['selected_items_list[]'].length && false == f.elements['all_items'].checked)
    {
        alert("Don't selected Items.");
        return false;
    }

    if ("" != f.elements['selected_items_list[]'].length && true == f.elements['all_items'].checked)
    {
        alert("if you want to select all items for discount please empty list of items you already selected.");
        return false;
    }

    if ("" == f.elements['discount_begin'].value)
    {
        alert("Discount Begin date is empty.");
        return false;
    }

    if ("" == f.elements['discount_ending'].value)
    {
        alert("Discount Ending date is empty.");
        return false;
    }

    z1 = f.elements['discount_ending'].value.split("/");
    z2 = f.elements['discount_begin'].value.split("/");
    if ((z1[2] + z1[0] + z1[1]) < (z2[2] + z2[0] + z2[1]))
    {
        alert("Date of discount start must be prior discount ends.");
        return false;
    }

    for (i = 0; i < f.elements['selected_items_list[]'].length; i++)
    {
        f.elements['selected_items_list[]'].options[i].selected = true;
    }

    
    return true;
}

</script>

<script language="JavaScript" type="text/javascript">
function ChangeDiv(num_div)
{
    switch(num_div)
    {
        case 1 : 
                document.getElementById("div_coupon").style.display = "inline";

                document.getElementById("div_dates").style.display = "none";
                document.getElementById("div_pr_count").style.display = "none";
                break;
        case 2 :
                document.getElementById("div_dates").style.display = "inline";

                document.getElementById("div_coupon").style.display = "none";
                document.getElementById("div_pr_count").style.display = "none";
                break;
        case 3 :
                document.getElementById("div_pr_count").style.display = "inline";

                document.getElementById("div_coupon").style.display = "none";
                document.getElementById("div_dates").style.display = "none";
                break;
    }
}
</script>
{/literal}

<div id="discounts">

<div class="title">{if 1 == $edit}Edit{else}Create{/if} A Discount<br /></div>

<input class="button" type="button" value="Coupon Discount" onClick="javascript : ChangeDiv(1);" {if 1 == $edit}disabled{/if} />
<input class="button" type="button" value="Dates Discount" onClick="javascript : ChangeDiv(2);" {if 1 == $edit}disabled{/if} />
<input class="button" type="button" value="Price/Count Discount" onClick="javascript : ChangeDiv(3);" {if 1 == $edit}disabled{/if} />

<div id="div_coupon" style="display : {if $data.type_form =='coupon'}inline{else}none{/if}">

<div class="title" style="text-align : center">Coupon Discount<br /></div>

<form name="form1" action="{$action_url}" method="post" onSubmit="javascript : return CheckData(1);">
  {if 1 == $edit}
  <input type="hidden" name="event" value="edit" />
  <input type="hidden" name="id_discount" value="{$data.id_discount}" />
  {else}
  <input type="hidden" name="event" value="add" />
  {/if}
  <input type="hidden" name="action" value="discount_coupon" />
    
  <table cellpadding="4" cellspacing="1" border="0" width="98%">
    <tr>
      <td align="right">Discount Title</td>
      <td><input type="text" name="discount_title" value="{$data.title}" /></td>
    </tr>
    <tr>
      <td align="right">Discount Code</td>
      <td><input type="text" name="discount_code" value="{$data.code_coupon}" /></td>
    </tr>
    <tr>
      <td align="right">Discount Amount</td>
      <td><input type="text" name="discount_amount" value="{$data.value_discount}" /></td>
    </tr>
    <tr>
      <td align="right">&nbsp;</td>
      <td>$<input type="radio" name="disc_am[]" value="0" {if $data.type_payment == "$" || $data.type_payment == ""}checked{/if} />
          %<input type="radio" name="disc_am[]" value="1" {if $data.type_payment == "%"}checked{/if} /></td>
    </tr>
    <tr>
      <td align="right" valign="top">Discount Type</td>
      <td>
        <select name="discount_type">
        {foreach from = $types_discounts item = type}
        <option value="{$type.id_type}"{if $type.id_type == $data.id_type} selected{/if}>{$type.title}</option>
        {/foreach}
        </select>
      </td>
    </tr>
    <tr>
      <td align="right" valign="top">Description</td>
      <td><textarea name="description" rows="7" cols="65">{$data.description}</textarea></td>
    </tr>
    <tr>
      <td align="right">Category and Items</td>
      <td>
      
      <table>
      <tr>
      <td colspan=3>

        <select name="catalog" onChange="javascript: do_list_items(1);">
        <option value="0">None</option>
        {foreach from = $arr_catalogs item = catalog}
            <option value="{$catalog.catalogid}">{$catalog.title}</option>
        {/foreach}
        </select>
        &nbsp;&nbsp;&nbsp;
        <input class="button" type="button" value=">>" align="left" onClick="javascript : AddCatalog(1);" />
      </td>
      </tr>
      <tr>

      <td>
<!--Select Items-->
        <div align="left">Target Items</div>
        <table align="left" >
        <tr>
            <td>
            <div >
            <select name="items_list[]" size=15 style="width:330px;" multiple>
            {foreach from = $arr_root_items item = product}
                <option value="{$product.catalogid}">{$product[0]} - {$product.title}</option>
            {/foreach}
            </select>
            </div>
            </td>
        </tr>
        </table>
      </td>

      <td>
          <input class="button" type="button" value=">>" onClick="javascript : AddItem(1);" />
          <br />
          <br />
          <br />
          <input class="button" type="button" value="<<" onClick="javascript : DellItem(1);" />
      </td>


      <td>
<!--Selected Items-->
        <div align="left">Selected Items</div>
        <table width="100%" align="left" >
        <tr>
            <td>
            <select name='selected_items_list[]' size=15 style="width:330px;" multiple>
            {foreach from = $arr_items item = product}
                <option value="{$product.catalogid}">{$product[0]} - {$product.title}</option>
            {/foreach}
            </select>
            </td>
        </tr>
        </table>
      </td>
      </tr>
      </table>
        
        <br />
        <input type="checkbox" name="all_items"{if "0" == $data.group_items} checked{/if} />&nbsp;All Items
      </td>
    </tr>
    
    
    <tr>
      <td align="right">Discount Begin</td>
      <td>
        <script language="javascript" type="text/javascript" src="{$http_root}/js/CalendarPopup.js"></script>
        <script language="JavaScript">document.write(getCalendarStyles());</script>
        <SCRIPT LANGUAGE="javascript">
            var cal1x = new CalendarPopup("calendar_view1");
        </SCRIPT>
        <input type="text" name="discount_begin" value="{$data.date_begin|date_format:'%m/%d/%C%Y'}" />
        <input class="button" type="button" value="Select date" name="select_date1" id="select_date1" onClick="cal1x.select(document.forms.form1.discount_begin,'select_date1','MM/dd/yyyy'); return false;" />
        <DIV ID="calendar_view1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>
      </td>
    </tr>
    <tr>
      <td align="right">Discount Ending</td>
      <td>
        <SCRIPT LANGUAGE="javascript">
            var cal2x = new CalendarPopup("calendar_view2");
        </SCRIPT>
        <input type="text" name="discount_ending" value="{$data.date_ending|date_format:'%m/%d/%C%Y'}" />
        <input class="button" type="button" value="Select date" name="select_date2" id="select_date2" onClick="cal2x.select(document.forms.form1.discount_ending,'select_date2','MM/dd/yyyy'); return false;" />
        <DIV ID="calendar_view2" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>
      </td>
    </tr>
    
    <tr>
    <td colspan=2><input class="button" type="submit" value="Submit" /></td>
    </tr>

  </table>  
</form>

</div>





<div id="div_dates" style="display :  {if $data.type_form =='date'}inline{else}none{/if}">

<div class="title" style="text-align : center">Dates Discount</div>

<form name="form2" action="{$action_url}" method="post" onSubmit="javascript : return CheckData(2);">
  {if 1 == $edit}
  <input type="hidden" name="event" value="edit" />
  <input type="hidden" name="id_discount" value="{$data.id_discount}" />
  {else}
  <input type="hidden" name="event" value="add" />
  {/if}
  <input type="hidden" name="action" value="discount_dates" />
    
  <table border="1" width="100%">
    <tr>
      <td align="right">Discount Title</td>
      <td><input type="text" name="discount_title" value="{$data.title}" /></td>
    </tr>
    <tr>
      <td align="right">Discount Amount</td>
      <td><input type="text" name="discount_amount" value="{$data.value_discount}" /></td>
    </tr>
    <tr>
      <td align="right">&nbsp;</td>
      <td>$<input type="radio" name="disc_am[]" value="0" {if $data.type_payment == "$" || $data.type_payment == ""}checked{/if} />
          %<input type="radio" name="disc_am[]" value="1" {if $data.type_payment == "%"}checked{/if} /></td>
    </tr>
    <tr>
      <td align="right" valign="top">Discount Type</td>
      <td>
        <select name="discount_type">
        {foreach from = $types_discounts item = type}
        <option value="{$type.id_type}"{if $type.id_type == $data.id_type} selected{/if}>{$type.title}</option>
        {/foreach}
        </select>
      </td>
    </tr>
    <tr>
      <td align="right" valign="top">Description</td>
      <td><textarea name="description" rows="7" cols="65">{$data.description}</textarea></td>
    </tr>
    <tr>
      <td align="right">Category and Items</td>
      <td>
      
      <table>
      <tr>
      <td colspan=3>

        <select name="catalog" onChange="javascript: do_list_items(2);">
        <option value="0">None</option>
        {foreach from = $arr_catalogs item = catalog}
            <option value="{$catalog.catalogid}">{$catalog.title}</option>
        {/foreach}
        </select>
        &nbsp;&nbsp;&nbsp;
        <input type="button" value=">>" align="left" onClick="javascript : AddCatalog(2);" />
      </td>
      </tr>
      <tr>

      <td>
<!--Select Items-->
        <div align="left">Target Items</div>
        <table align="left" id="tbl">
        <tr>
            <td>
            <div id="items">
            <select name="items_list[]" size=15 style="width:330px;" multiple>
            {foreach from = $arr_root_items item = product}
                <option value="{$product.catalogid}">{$product[0]} - {$product.title}</option>
            {/foreach}
            </select>
            </div>
            </td>
        </tr>
        </table>
      </td>

      <td>
          <input type="button" value=">>" onClick="javascript : AddItem(2);" />
          <br />
          <br />
          <br />
          <input type="button" value="<<" onClick="javascript : DellItem(2);" />
      </td>


      <td>
<!--Selected Items-->
        <div align="left">Selected Items</div>
        <table width="100%" align="left" id="tbl">
        <tr>
            <td>
            <select name='selected_items_list[]' size=15 style="width:330px;" multiple>
            {foreach from = $arr_items item = product}
                <option value="{$product.catalogid}">{$product[0]} - {$product.title}</option>
            {/foreach}
            </select>
            </td>
        </tr>
        </table>
      </td>
      </tr>
      </table>
      
        <br />
        <input type="checkbox" name="all_items"{if "0" == $data.group_items} checked{/if} />&nbsp;All Items
      </td>
    </tr>
    
    
    <tr>
      <td align="right">Discount Begin</td>
      <td>
        <SCRIPT LANGUAGE="javascript">
            var ca21x = new CalendarPopup("calendar_view12");
        </SCRIPT>
        <input type="text" name="discount_begin" value="{$data.date_begin|date_format:'%m/%d/%C%Y'}" />
        <input type="button" value="Select date" name="select_date12" id="select_date12" onClick="ca21x.select(document.forms.form2.discount_begin,'select_date12','MM/dd/yyyy'); return false;" />
        <DIV ID="calendar_view12" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>
      </td>
    </tr>
    <tr>
      <td align="right">Discount Ending</td>
      <td>
        <SCRIPT LANGUAGE="javascript">
            var ca22x = new CalendarPopup("calendar_view22");
        </SCRIPT>
        <input type="text" name="discount_ending" value="{$data.date_ending|date_format:'%m/%d/%C%Y'}" />
        <input type="button" value="Select date" name="select_date22" id="select_date22" onClick="ca22x.select(document.forms.form2.discount_ending,'select_date22','MM/dd/yyyy'); return false;" />
        <DIV ID="calendar_view22" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>
      </td>
    </tr>
    
    <tr>
    <td colspan=2><input type="submit" value="Submit" /></td>
    </tr>

  </table>  
</form>

</div>




<div id="div_pr_count" style="display : {if $data.type_form =='price/count discount'}inline{else}none{/if}">

<div class="title" style="text-align : center">Price/Count Discount</div>

<form name="form3" action="{$action_url}" method="post" onSubmit="javascript : return CheckData(3);">
  {if 1 == $edit}
  <input type="hidden" name="event" value="edit" />
  <input type="hidden" name="id_discount" value="{$data.id_discount}" />
  {else}
  <input type="hidden" name="event" value="add" />
  {/if}
  <input type="hidden" name="action" value="discount_pr_count" />
    
  <table border="1" width="100%">
    <tr>
      <td align="right">Discount Title</td>
      <td><input type="text" name="discount_title" value="{$data.title}" /></td>
    </tr>
    <tr>
      <td align="right">Discount Amount</td>
      <td><input type="text" name="discount_amount" value="{$data.value_discount}" /></td>
    </tr>
    <tr>
      <td align="right">&nbsp;</td>
      <td>$<input type="radio" name="disc_am[]" value="0" {if $data.type_payment == "$" || $data.type_payment == ""}checked{/if} />
          %<input type="radio" name="disc_am[]" value="1" {if $data.type_payment == "%"}checked{/if} /></td>
    </tr>
    <tr>
      <td align="right">Discount Price/Count</td>
      <td><input type="text" name="discount_pr_count" value="{$data.amount_buying}" /></td>
    </tr>
    <tr>
      <td align="right">&nbsp;</td>
      <td>Price $<input type="radio" name="p_c[]" value="0" {if $data.type_amount_buying == "$" || $data.type_payment == ""}checked{/if} />
          Amount<input type="radio" name="p_c[]" value="1" {if $data.type_amount_buying == "a"}checked{/if} /></td>
    </tr>
    <tr>
      <td align="right" valign="top">Discount Type</td>
      <td>
        <select name="discount_type">
        {foreach from = $types_discounts item = type}
        <option value="{$type.id_type}"{if $type.id_type == $data.id_type} selected{/if}>{$type.title}</option>
        {/foreach}
        </select>
      </td>
    </tr>
    <tr>
      <td align="right" valign="top">Description</td>
      <td><textarea name="description" rows="7" cols="65">{$data.description}</textarea></td>
    </tr>
    <tr>
      <td align="right">Category and Items</td>
      <td>
      
      <table>
      <tr>
      <td colspan=3>

        <select name="catalog" onChange="javascript: do_list_items(3);">
        <option value="0">None</option>
        {foreach from = $arr_catalogs item = catalog}
            <option value="{$catalog.catalogid}">{$catalog.title}</option>
        {/foreach}
        </select>
        &nbsp;&nbsp;&nbsp;
        <input type="button" value=">>" align="left" onClick="javascript : AddCatalog(3);" />
      </td>
      </tr>
      <tr>

      <td>
<!--Select Items-->
        <div align="left">Target Items</div>
        <table align="left" id="tbl">
        <tr>
            <td>
            <div id="items">
            <select name="items_list[]" size=15 style="width:330px;" multiple>
            {foreach from = $arr_root_items item = product}
                <option value="{$product.catalogid}">{$product[0]} - {$product.title}</option>
            {/foreach}
            </select>
            </div>
            </td>
        </tr>
        </table>
      </td>

      <td>
          <input type="button" value=">>" onClick="javascript : AddItem(3);" />
          <br />
          <br />
          <br />
          <input type="button" value="<<" onClick="javascript : DellItem(3);" />
      </td>


      <td>
<!--Selected Items-->
        <div align="left">Selected Items</div>
        <table width="100%" align="left" id="tbl">
        <tr>
            <td>
            <select name='selected_items_list[]' size=15 style="width:330px;" multiple>
            {foreach from = $arr_items item = product}
                <option value="{$product.catalogid}">{$product[0]} - {$product.title}</option>
            {/foreach}
            </select>
            </td>
        </tr>
        </table>
      </td>
      </tr>
      </table>
      
        <br />
        <input type="checkbox" name="all_items"{if "0" == $data.group_items} checked{/if} />&nbsp;All Items
      </td>
    </tr>
    
    
    <tr>
      <td align="right">Discount Begin</td>
      <td>
        <SCRIPT LANGUAGE="javascript">
            var ca31x = new CalendarPopup("calendar_view13");
        </SCRIPT>
        <input type="text" name="discount_begin" value="{$data.date_begin|date_format:'%m/%d/%C%Y'}" />
        <input type="button" value="Select date" name="select_date13" id="select_date13" onClick="ca31x.select(document.forms.form3.discount_begin,'select_date13','MM/dd/yyyy'); return false;" />
        <DIV ID="calendar_view13" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>
      </td>
    </tr>
    <tr>
      <td align="right">Discount Ending</td>
      <td>
        <SCRIPT LANGUAGE="javascript">
            var ca32x = new CalendarPopup("calendar_view23");
        </SCRIPT>
        <input type="text" name="discount_ending" value="{$data.date_ending|date_format:'%m/%d/%C%Y'}" />
        <input type="button" value="Select date" name="select_date23" id="select_date23" onClick="ca32x.select(document.forms.form3.discount_ending,'select_date23','MM/dd/yyyy'); return false;" />
        <DIV ID="calendar_view23" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>
      </td>
    </tr>
    
    <tr>
    <td colspan=2><input type="submit" value="Submit" /></td>
    </tr>

  </table>  
</form>

</div>
 </div>